package com.daqsoft.service.process;

import com.daqsoft.data.User;
import com.daqsoft.data.project.Attachment;
import com.daqsoft.data.project.Project;
import com.daqsoft.data.project.Task;
import com.daqsoft.dto.StatusEnum;
import com.daqsoft.dto.TaskTypeEnum;
import com.daqsoft.service.project.AttachmentService;
import com.daqsoft.service.project.TaskService;
import com.daqsoft.util.DateUtil;
import com.daqsoft.util.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 周麟东 on 16:27
 */
@Service
public class ProjectFileProcessService {

    @Autowired
    private TaskService taskService;
    @Autowired
    private AttachmentService attachmentService;

    /**
     * 提交项目文件新增请求
     *
     * @param verifier  提交人
     * @param principal 审核人
     * @param project   项目
     * @return 是否提交成功
     */
    public Task submitAdd(User verifier, User principal, Project project, String desc, Integer id) {
        //首先查询该项目有没有文件新增的任务
        List<Task> tasks = taskService.getByProjectAndType(project, TaskTypeEnum.PROJECT_FILE_UPDATE);
        if (tasks != null) {
            for (Task task : tasks) {
                if (task.getStatus() == StatusEnum.PROCESS || task.getStatus() == StatusEnum.CHECK) {
                    return null;
                }
            }
        }
        //增加任务
        Task task = new Task();
        if (id != null && id != 0) {
            task = taskService.get(id);
        }
        task.setName("项目文件新增请求");
        task.setMsg(Log.format(principal, "提交项目文件新增请求"));
        task.setPrincipal(principal);
        task.setVerifier(verifier);
        task.setDesc(desc);
        task.setStatus(StatusEnum.CHECK);
        task.setStartDate(new Date());
        task.setEndDate(DateUtil.INS.addDay(new Date(), 3));
        task.setProject(project);
        task.setType(TaskTypeEnum.PROJECT_FILE_UPDATE);
        taskService.save(task);
        return task;
    }

    /**
     * 提交删除项目文件请求
     * @param verifier 审核人
     * @param principal 提交人
     * @param project 项目对象
     * @param desc 说明
     * @param id 任务ID
     * @param deleteIds 要删除的文件ID
     * @return 任务 若返回空则代表提交失败
     */
    public Task submitDelete(User verifier, User principal, Project project, String desc, Integer id, int[] deleteIds) {
        //首先查询该项目有没有文件新增的任务
        List<Task> tasks = taskService.getByProjectAndType(project, TaskTypeEnum.PROJECT_FILE_DELETE);
        if (tasks != null) {
            for (Task task : tasks) {
                if (task.getStatus() == StatusEnum.PROCESS || task.getStatus() == StatusEnum.CHECK) {
                    return null;
                }
            }
        }
        StringBuilder ids = new StringBuilder();
        for (Integer deleteId : deleteIds) {
            ids.append(deleteId).append(",");
        }
        Task task = new Task();
        if (id != null && id != 0) {
            task = taskService.get(id);
        }
        task.setName("项目文件删除请求");
        task.setMsg(Log.format(principal, "提交项目文件删除请求"));
        task.setSolution(ids.toString());
        task.setPrincipal(principal);
        task.setVerifier(verifier);
        task.setDesc(desc);
        task.setStatus(StatusEnum.CHECK);
        task.setStartDate(new Date());
        task.setEndDate(DateUtil.INS.addDay(new Date(), 3));
        task.setProject(project);
        task.setType(TaskTypeEnum.PROJECT_FILE_DELETE);
        taskService.save(task);
        return task;
    }

    /**
     * 审核增加项目文件任务
     *
     * @param task   任务
     * @param status 状态
     */
    @Transactional
    public void checkAdd(Task task, String status) {
        StatusEnum statusEnum = StatusEnum.valueOf(status);
        task.setStatus(statusEnum);
        if (statusEnum == StatusEnum.ERROR) {
            //直接保存
            taskService.save(task);
        }
        if (statusEnum == StatusEnum.FINISH) {
            //移动文件
            for (Attachment attachment : task.getAttachments()) {
                attachment.setProjectFile(true);
                attachment.setTask(null);
                attachmentService.save(attachment);
            }
            task.setRealEndDate(new Date());
            //保存任务
            taskService.save(task);
        }
    }

    /**
     * 审核删除项目文件任务
     *
     * @param task   任务
     * @param status 状态
     */
    @Transactional
    public void checkDelete(Task task, String status) {
        StatusEnum statusEnum = StatusEnum.valueOf(status);
        task.setStatus(statusEnum);
        if (statusEnum == StatusEnum.ERROR) {
            //直接保存
            taskService.save(task);
        }
        if (statusEnum == StatusEnum.FINISH) {
            //删除文件
            String [] ids = task.getSolution().split(",");
            for (String id : ids) {
                attachmentService.delete(Integer.valueOf(id));
            }
            task.setRealEndDate(new Date());
            //保存任务
            taskService.save(task);
        }
    }
}
